/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.core.rtgen.lcpgenerator.method;

import com.inspur.edp.bef.bizentity.operation.BizAction;
import com.inspur.edp.bef.bizentity.operation.BizMgrAction;
import com.inspur.edp.bef.component.base.VoidReturnType;
import com.inspur.edp.bef.component.detailcmpentity.be.BEComponent;
import com.inspur.edp.bef.component.detailcmpentity.be.BEMethod;
import com.inspur.edp.bef.component.detailcmpentity.be.BEMethodParameter;
import com.inspur.edp.bef.component.detailcmpentity.bemgr.BEMgrComponent;
import com.inspur.edp.bef.component.detailcmpentity.bemgr.BEMgrMethod;
import com.inspur.edp.bef.component.detailcmpentity.bemgr.BEMgrMethodParameter;
import com.inspur.edp.bef.dtgenerator.common.Utils;
import com.inspur.edp.bef.dtgenerator.core.BefCoreGenCtx;
import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import java.util.ArrayList;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.CastExpression;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;

public class LcpTransGenerator extends ClassMethodGenerator
{
	private static final String TransMethodName = "getBEManager";
	private static final String VariableName = "trans";
	private final BizMgrAction operation;
	private final BefCoreGenCtx coreGenCtx;
	private BEMgrComponent beComp;
	private BEMgrMethod beMethod;
	private MediateType belongType;

	public LcpTransGenerator(MediateType belongType, BizMgrAction operation, BefCoreGenCtx coreGenCtx)
	{
		this.operation = operation;
		this.coreGenCtx = coreGenCtx;
		this.belongType = belongType;

		GspMetadata metadata =
				coreGenCtx
						.getBeGenCtx()
						.getMetadataPool()
						.getMetadata(operation.getComponentPkgName(), operation.getComponentId());
		beComp = (BEMgrComponent) metadata.getContent();
		if (beComp == null) {
			throw new RuntimeException(
					/*ErrorCode.BeCompNotFound*/
					Utils.formatExceptionMsg("BE动作构件不存在: " + operation.getComponentId()));
		}
		beMethod = beComp.getBeMgrMethod();
	}

	@Override
	protected String getMethodName()
	{
		return operation.getCode();
	}

	@Override
	protected TypeInfo getReturnType() {
		try{
			return com.inspur.edp.cef.generator.overall.Utils.getReturnType(beMethod, coreGenCtx.getBeGenCtx());
		}catch (Exception e){
			throw new RuntimeException("方法"+operation.getCode()+"返回值解析异常", e);
		}
	}

	@Override
	protected java.util.ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
		return
				new java.util.ArrayList<Modifier.ModifierKeyword>(java.util.Arrays.asList(new Modifier.ModifierKeyword[]{Modifier.ModifierKeyword.PUBLIC_KEYWORD}));
	}

	@Override
	protected ArrayList<ParameterInfo> getParameterCollection() {
		java.util.ArrayList<ParameterInfo> result = new java.util.ArrayList<ParameterInfo>();
		for (BEMgrMethodParameter par : beMethod.getParameterCollection()) {
			result.add(com.inspur.edp.cef.generator.overall.Utils.buildParGenInfo(par, coreGenCtx.getBeGenCtx(), null));
		}
		return result;
	}

	@Override
	protected Block buildMethodBody() {
		Block block=ast.newBlock();
		addNewServiceStatement(block);
		addInvokeMethodStatemetn(block);
		return block;
	}

	private void addNewServiceStatement(Block block) {
		block.statements().add(buildNewServiceStatement(ast, belongType, VariableName));
	}

	static Statement buildNewServiceStatement(AST ast, MediateType belongType, String variableName){
		VariableDeclarationFragment variableDeclarationFragment=ast.newVariableDeclarationFragment();
		variableDeclarationFragment.setName(ast.newSimpleName(variableName));
		addNewServiceInitializer(ast, belongType, variableDeclarationFragment);
		VariableDeclarationStatement variableDeclarationStatement=ast.newVariableDeclarationStatement(variableDeclarationFragment);
		variableDeclarationStatement.setType(new com.inspur.edp.caf.generator.baseInfo.TypeInfo(belongType.getFullName()).getType(ast));
		return variableDeclarationStatement;
	}

	private static void addNewServiceInitializer(AST ast, MediateType belongType, VariableDeclarationFragment variableDeclarationFragment) {
		CastExpression castExpression=ast.newCastExpression();
		addCastManagerExpression(ast, castExpression);
		castExpression.setType(new com.inspur.edp.caf.generator.baseInfo.TypeInfo(belongType.getFullName()).getType(ast));
		variableDeclarationFragment.setInitializer(castExpression);
	}

	private static void addCastManagerExpression(AST ast, CastExpression castExpression) {
		MethodInvocation methodInvocation=ast.newMethodInvocation();
		methodInvocation.setName(ast.newSimpleName(TransMethodName));
		methodInvocation.setExpression(ast.newThisExpression());
		castExpression.setExpression(methodInvocation);
	}

	private void addInvokeMethodStatemetn(Block block) {
		MethodInvocation methodInvocation=ast.newMethodInvocation();
		methodInvocation.setName(ast.newSimpleName(operation.getCode()));
		addInvokeMethodParameters(methodInvocation);
		methodInvocation.setExpression(ast.newSimpleName(VariableName));

		if(beMethod.getReturnValue() instanceof VoidReturnType)
		{
			block.statements().add(ast.newExpressionStatement(methodInvocation));
		}
		else {
			ReturnStatement returnStatement=ast.newReturnStatement();
			returnStatement.setExpression(methodInvocation);
			block.statements().add(returnStatement);
		}
	}

	private void addInvokeMethodParameters(MethodInvocation methodInvocation) {
		for (BEMgrMethodParameter parameter:beMethod.getParameterCollection())
		{
			methodInvocation.arguments().add(ast.newSimpleName(parameter.getParamCode()));
		}
	}

  @Override
  protected boolean getIsOverride() {
    return true;
  }
}
